using System;
using System.Globalization;
using System.IO;
using System.Xml;
using Newtonsoft.Json;

namespace ns20
{
	internal static class Class185
	{
		private const int int_0 = 36524;

		private const int int_1 = 146097;

		private const int int_2 = 1461;

		private const int int_3 = 365;

		private const long long_0 = 864000000000L;

		internal static readonly long long_1;

		private static readonly int[] int_4;

		private static readonly int[] int_5;

		static Class185()
		{
			long_1 = 621355968000000000L;
			int_4 = new int[13]
			{
				0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
				304, 334, 365
			};
			int_5 = new int[13]
			{
				0, 31, 60, 91, 121, 152, 182, 213, 244, 274,
				305, 335, 366
			};
		}

		public static TimeSpan smethod_0(this DateTime dateTime_0)
		{
			return TimeZoneInfo.Local.GetUtcOffset(dateTime_0);
		}

		public static XmlDateTimeSerializationMode smethod_1(DateTimeKind dateTimeKind_0)
		{
			return dateTimeKind_0 switch
			{
				DateTimeKind.Unspecified => XmlDateTimeSerializationMode.Unspecified, 
				DateTimeKind.Utc => XmlDateTimeSerializationMode.Utc, 
				DateTimeKind.Local => XmlDateTimeSerializationMode.Local, 
				_ => throw Class194.smethod_1("kind", dateTimeKind_0, "Unexpected DateTimeKind value."), 
			};
		}

		internal static DateTime smethod_2(DateTime dateTime_0, DateTimeZoneHandling dateTimeZoneHandling_0)
		{
			switch (dateTimeZoneHandling_0)
			{
			default:
				throw new ArgumentException("Invalid date time handling value.");
			case DateTimeZoneHandling.Local:
				dateTime_0 = smethod_3(dateTime_0);
				break;
			case DateTimeZoneHandling.Utc:
				dateTime_0 = smethod_4(dateTime_0);
				break;
			case DateTimeZoneHandling.Unspecified:
				dateTime_0 = new DateTime(dateTime_0.Ticks, DateTimeKind.Unspecified);
				break;
			case DateTimeZoneHandling.RoundtripKind:
				break;
			}
			return dateTime_0;
		}

		private static DateTime smethod_3(DateTime dateTime_0)
		{
			return dateTime_0.Kind switch
			{
				DateTimeKind.Unspecified => new DateTime(dateTime_0.Ticks, DateTimeKind.Local), 
				DateTimeKind.Utc => dateTime_0.ToLocalTime(), 
				DateTimeKind.Local => dateTime_0, 
				_ => dateTime_0, 
			};
		}

		private static DateTime smethod_4(DateTime dateTime_0)
		{
			return dateTime_0.Kind switch
			{
				DateTimeKind.Unspecified => new DateTime(dateTime_0.Ticks, DateTimeKind.Utc), 
				DateTimeKind.Utc => dateTime_0, 
				DateTimeKind.Local => dateTime_0.ToUniversalTime(), 
				_ => dateTime_0, 
			};
		}

		private static long smethod_5(DateTime dateTime_0)
		{
			if (dateTime_0.Kind == DateTimeKind.Utc)
			{
				return dateTime_0.Ticks;
			}
			return smethod_6(dateTime_0, dateTime_0.smethod_0());
		}

		private static long smethod_6(DateTime dateTime_0, TimeSpan timeSpan_0)
		{
			if (dateTime_0.Kind != DateTimeKind.Utc && !(dateTime_0 == DateTime.MaxValue) && !(dateTime_0 == DateTime.MinValue))
			{
				long num = dateTime_0.Ticks - timeSpan_0.Ticks;
				if (num > 3155378975999999999L)
				{
					return 3155378975999999999L;
				}
				if (num < 0L)
				{
					return 0L;
				}
				return num;
			}
			return dateTime_0.Ticks;
		}

		internal static long smethod_7(DateTime dateTime_0, TimeSpan timeSpan_0)
		{
			long long_ = smethod_6(dateTime_0, timeSpan_0);
			return smethod_10(long_);
		}

		internal static long smethod_8(DateTime dateTime_0)
		{
			return smethod_9(dateTime_0, bool_0: true);
		}

		internal static long smethod_9(DateTime dateTime_0, bool bool_0)
		{
			long long_ = (bool_0 ? smethod_5(dateTime_0) : dateTime_0.Ticks);
			return smethod_10(long_);
		}

		private static long smethod_10(long long_2)
		{
			return (long_2 - long_1) / 10000L;
		}

		internal static DateTime smethod_11(long long_2)
		{
			return new DateTime(long_2 * 10000L + long_1, DateTimeKind.Utc);
		}

		internal static bool smethod_12(string string_0, DateParseHandling dateParseHandling_0, DateTimeZoneHandling dateTimeZoneHandling_0, out object object_0)
		{
			Struct26 @struct = default(Struct26);
			if (!@struct.method_0(string_0))
			{
				object_0 = null;
				return false;
			}
			DateTime dateTime = new DateTime(@struct.int_0, @struct.int_1, @struct.int_2, @struct.int_3, @struct.int_4, @struct.int_5).AddTicks(@struct.int_6);
			if (dateParseHandling_0 == DateParseHandling.DateTimeOffset)
			{
				TimeSpan offset = @struct.enum13_0 switch
				{
					Enum13.Utc => new TimeSpan(0L), 
					Enum13.LocalWestOfUtc => new TimeSpan(-@struct.int_7, -@struct.int_8, 0), 
					Enum13.LocalEastOfUtc => new TimeSpan(@struct.int_7, @struct.int_8, 0), 
					_ => TimeZoneInfo.Local.GetUtcOffset(dateTime), 
				};
				long num = dateTime.Ticks - offset.Ticks;
				if (num >= 0L && num <= 3155378975999999999L)
				{
					object_0 = new DateTimeOffset(dateTime, offset);
					return true;
				}
				object_0 = null;
				return false;
			}
			switch (@struct.enum13_0)
			{
			case Enum13.Utc:
				dateTime = new DateTime(dateTime.Ticks, DateTimeKind.Utc);
				break;
			case Enum13.LocalWestOfUtc:
			{
				TimeSpan timeSpan2 = new TimeSpan(@struct.int_7, @struct.int_8, 0);
				long num2 = dateTime.Ticks + timeSpan2.Ticks;
				if (num2 <= DateTime.MaxValue.Ticks)
				{
					dateTime = new DateTime(num2, DateTimeKind.Utc).ToLocalTime();
					break;
				}
				num2 += dateTime.smethod_0().Ticks;
				if (num2 > DateTime.MaxValue.Ticks)
				{
					num2 = DateTime.MaxValue.Ticks;
				}
				dateTime = new DateTime(num2, DateTimeKind.Local);
				break;
			}
			case Enum13.LocalEastOfUtc:
			{
				TimeSpan timeSpan = new TimeSpan(@struct.int_7, @struct.int_8, 0);
				long num2 = dateTime.Ticks - timeSpan.Ticks;
				if (num2 >= DateTime.MinValue.Ticks)
				{
					dateTime = new DateTime(num2, DateTimeKind.Utc).ToLocalTime();
					break;
				}
				num2 += dateTime.smethod_0().Ticks;
				if (num2 < DateTime.MinValue.Ticks)
				{
					num2 = DateTime.MinValue.Ticks;
				}
				dateTime = new DateTime(num2, DateTimeKind.Local);
				break;
			}
			}
			object_0 = smethod_2(dateTime, dateTimeZoneHandling_0);
			return true;
		}

		internal static bool smethod_13(string string_0, DateParseHandling dateParseHandling_0, DateTimeZoneHandling dateTimeZoneHandling_0, out object object_0)
		{
			if (string_0.Length > 0)
			{
				if (string_0[0] == '/')
				{
					if (string_0.StartsWith("/Date(", StringComparison.Ordinal) && string_0.EndsWith(")/", StringComparison.Ordinal))
					{
						return smethod_14(string_0, dateParseHandling_0, dateTimeZoneHandling_0, out object_0);
					}
				}
				else if (string_0.Length >= 19 && string_0.Length <= 40 && char.IsDigit(string_0[0]) && string_0[10] == 'T')
				{
					return smethod_12(string_0, dateParseHandling_0, dateTimeZoneHandling_0, out object_0);
				}
			}
			object_0 = null;
			return false;
		}

		private static bool smethod_14(string string_0, DateParseHandling dateParseHandling_0, DateTimeZoneHandling dateTimeZoneHandling_0, out object object_0)
		{
			string text = string_0.Substring(6, string_0.Length - 8);
			DateTimeKind dateTimeKind = DateTimeKind.Utc;
			int num = text.IndexOf('+', 1);
			if (num == -1)
			{
				num = text.IndexOf('-', 1);
			}
			TimeSpan timeSpan = TimeSpan.Zero;
			if (num != -1)
			{
				dateTimeKind = DateTimeKind.Local;
				timeSpan = smethod_15(text.Substring(num));
				text = text.Substring(0, num);
			}
			long long_ = long.Parse(text, NumberStyles.Integer, CultureInfo.InvariantCulture);
			DateTime dateTime = smethod_11(long_);
			if (dateParseHandling_0 == DateParseHandling.DateTimeOffset)
			{
				object_0 = new DateTimeOffset(dateTime.Add(timeSpan).Ticks, timeSpan);
				return true;
			}
			object_0 = smethod_2(dateTimeKind switch
			{
				DateTimeKind.Unspecified => DateTime.SpecifyKind(dateTime.ToLocalTime(), DateTimeKind.Unspecified), 
				DateTimeKind.Local => dateTime.ToLocalTime(), 
				_ => dateTime, 
			}, dateTimeZoneHandling_0);
			return true;
		}

		private static TimeSpan smethod_15(string string_0)
		{
			bool flag = string_0[0] == '-';
			int num = int.Parse(string_0.Substring(1, 2), NumberStyles.Integer, CultureInfo.InvariantCulture);
			int num2 = 0;
			if (string_0.Length >= 5)
			{
				num2 = int.Parse(string_0.Substring(3, 2), NumberStyles.Integer, CultureInfo.InvariantCulture);
			}
			TimeSpan result = TimeSpan.FromHours(num) + TimeSpan.FromMinutes(num2);
			if (flag)
			{
				result = result.Negate();
			}
			return result;
		}

		internal static void smethod_16(TextWriter textWriter_0, DateTime dateTime_0, DateFormatHandling dateFormatHandling_0, string string_0, CultureInfo cultureInfo_0)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				char[] array = new char[64];
				int count = smethod_17(array, 0, dateTime_0, null, dateTime_0.Kind, dateFormatHandling_0);
				textWriter_0.Write(array, 0, count);
			}
			else
			{
				textWriter_0.Write(dateTime_0.ToString(string_0, cultureInfo_0));
			}
		}

		internal static int smethod_17(char[] char_0, int int_6, DateTime dateTime_0, TimeSpan? nullable_0, DateTimeKind dateTimeKind_0, DateFormatHandling dateFormatHandling_0)
		{
			int num = int_6;
			if (dateFormatHandling_0 == DateFormatHandling.MicrosoftDateFormat)
			{
				TimeSpan timeSpan_ = nullable_0 ?? dateTime_0.smethod_0();
				long num2 = smethod_7(dateTime_0, timeSpan_);
				"\\/Date(".CopyTo(0, char_0, num, 7);
				num += 7;
				string text = num2.ToString(CultureInfo.InvariantCulture);
				text.CopyTo(0, char_0, num, text.Length);
				num += text.Length;
				switch (dateTimeKind_0)
				{
				case DateTimeKind.Unspecified:
					if (dateTime_0 != DateTime.MaxValue && dateTime_0 != DateTime.MinValue)
					{
						num = smethod_20(char_0, num, timeSpan_, dateFormatHandling_0);
					}
					break;
				case DateTimeKind.Local:
					num = smethod_20(char_0, num, timeSpan_, dateFormatHandling_0);
					break;
				}
				")\\/".CopyTo(0, char_0, num, 3);
				num += 3;
			}
			else
			{
				num = smethod_18(char_0, num, dateTime_0);
				switch (dateTimeKind_0)
				{
				case DateTimeKind.Utc:
					char_0[num++] = 'Z';
					break;
				case DateTimeKind.Local:
					num = smethod_20(char_0, num, nullable_0 ?? dateTime_0.smethod_0(), dateFormatHandling_0);
					break;
				}
			}
			return num;
		}

		internal static int smethod_18(char[] char_0, int int_6, DateTime dateTime_0)
		{
			int num = 19;
			smethod_22(dateTime_0, out var int_7, out var int_8, out var int_9);
			smethod_19(char_0, int_6, int_7, 4);
			char_0[int_6 + 4] = '-';
			smethod_19(char_0, int_6 + 5, int_8, 2);
			char_0[int_6 + 7] = '-';
			smethod_19(char_0, int_6 + 8, int_9, 2);
			char_0[int_6 + 10] = 'T';
			smethod_19(char_0, int_6 + 11, dateTime_0.Hour, 2);
			char_0[int_6 + 13] = ':';
			smethod_19(char_0, int_6 + 14, dateTime_0.Minute, 2);
			char_0[int_6 + 16] = ':';
			smethod_19(char_0, int_6 + 17, dateTime_0.Second, 2);
			int num2 = (int)(dateTime_0.Ticks % 10000000L);
			if (num2 != 0)
			{
				int num3 = 7;
				while (num2 % 10 == 0)
				{
					num3--;
					num2 /= 10;
				}
				char_0[int_6 + 19] = '.';
				smethod_19(char_0, int_6 + 20, num2, num3);
				num += num3 + 1;
			}
			return int_6 + num;
		}

		private static void smethod_19(char[] char_0, int int_6, int int_7, int int_8)
		{
			while (int_8-- != 0)
			{
				char_0[int_6 + int_8] = (char)(int_7 % 10 + 48);
				int_7 /= 10;
			}
		}

		internal static int smethod_20(char[] char_0, int int_6, TimeSpan timeSpan_0, DateFormatHandling dateFormatHandling_0)
		{
			char_0[int_6++] = ((timeSpan_0.Ticks >= 0L) ? '+' : '-');
			int int_7 = Math.Abs(timeSpan_0.Hours);
			smethod_19(char_0, int_6, int_7, 2);
			int_6 += 2;
			if (dateFormatHandling_0 == DateFormatHandling.IsoDateFormat)
			{
				char_0[int_6++] = ':';
			}
			int int_8 = Math.Abs(timeSpan_0.Minutes);
			smethod_19(char_0, int_6, int_8, 2);
			int_6 += 2;
			return int_6;
		}

		internal static void smethod_21(TextWriter textWriter_0, DateTimeOffset dateTimeOffset_0, DateFormatHandling dateFormatHandling_0, string string_0, CultureInfo cultureInfo_0)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				char[] array = new char[64];
				int count = smethod_17(array, 0, (dateFormatHandling_0 == DateFormatHandling.IsoDateFormat) ? dateTimeOffset_0.DateTime : dateTimeOffset_0.UtcDateTime, dateTimeOffset_0.Offset, DateTimeKind.Local, dateFormatHandling_0);
				textWriter_0.Write(array, 0, count);
			}
			else
			{
				textWriter_0.Write(dateTimeOffset_0.ToString(string_0, cultureInfo_0));
			}
		}

		private static void smethod_22(DateTime dateTime_0, out int int_6, out int int_7, out int int_8)
		{
			long ticks = dateTime_0.Ticks;
			int num = (int)(ticks / 864000000000L);
			int num2 = num / 146097;
			num -= num2 * 146097;
			int num3 = num / 36524;
			if (num3 == 4)
			{
				num3 = 3;
			}
			num -= num3 * 36524;
			int num4 = num / 1461;
			num -= num4 * 1461;
			int num5 = num / 365;
			if (num5 == 4)
			{
				num5 = 3;
			}
			int_6 = num2 * 400 + num3 * 100 + num4 * 4 + num5 + 1;
			num -= num5 * 365;
			int[] array = ((num5 == 3 && (num4 != 24 || num3 == 3)) ? int_5 : int_4);
			int i;
			for (i = num >> 6; num >= array[i]; i++)
			{
			}
			int_7 = i;
			int_8 = num - array[i - 1] + 1;
		}
	}
}
